<?php
/**
 * 推广管理
 */
namespace App\Controller\Member;
use Common\Controller\App\CommonController;
use Common\Model\MemberModel;
use Common\Model\MicroShopExpandMaterialModel;
use Common\Model\OrderModel;
use Common\Model\ProductSkuModel;
use Common\Model\ProductsModel;
use Common\Model\ShareSpecialModel;

class ShareSpecialsController extends CommonController{
    /**
     * 文章列表
     */
    public function index(){
        $page = I('post.page')?I('post.page'):1;
        $limit = I('page.limit')?I('page.limit'):20;
        $num = ($page-1)*$limit;
        $model = new ShareSpecialModel();
        $fields = ['id', 'title', 'pic', 'visit_num', 'create_time'];
        $list = $model->where("status>0")->order("displayorder asc,create_time desc")->field($fields)->limit($num,$limit)->select();
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }

    /**
     * 文章详情
     */
    public function getInfo(){
        $id = I('post.id');
        if(empty($id)){
            $this->ajaxReturn(['code'=>300,'msg'=>'文章编号错误']);
        }
        $model = new ShareSpecialModel();
        $fields = ['id', 'title', 'desc', 'pic', 'visit_num', 'create_time', 'share_title', 'share_desc', 'share_pic'];

        $info = $model->where("id=$id")->field($fields)->find();
        $this->ajaxReturn(['code'=>300,'info'=>$info]);
    }

    /**
     * 招商素材列表
     */
    public function z_list(){
        $page = I('post.page')?I('post.page'):1;
        $limit = I('page.limit')?I('page.limit'):20;
        $num = ($page-1)*$limit;
        $model = new MicroShopExpandMaterialModel();
        $list = $model->where("status>0")->order("id desc")->limit($num,$limit)->select();
        foreach($list as $k=>$v){
            $pic = json_decode($v['pic'],true);
            $list[$k]['pic'] = $pic;
        }
        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }

    /**
     * 产品素材
     */
    public function p_list(){
        $page = I('post.page')?I('post.page'):1;
        $limit = I('page.limit')?I('page.limit'):20;
        $num = ($page-1)*$limit;
        $model = new ProductsModel();
        $fields = ['id', 'pname', 'promotion_tips', 'short_desc', 'sys_update_time'];

        $list = $model->where("ms_status>0 and ms_has_material=1")->order("sys_update_time desc")->field($fields)->limit($num,$limit)->select();
        $model = new ProductSkuModel();
        foreach($list as $k=>$v){
            $info = $model->where("pid=".$v['id']." and ms_status>0")->field('pic')->order("pid desc")->find();
            $pic = explode(',',$info['pic']);
            $list[$k]['pic'] = $pic;
        }

        $this->ajaxReturn(['code'=>200,'list'=>$list]);
    }

}